package com.snowy.hidden.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.snowy.framework.common.base.vo.LoginUser;
import com.snowy.hidden.user.domain.model.AppletUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snowy.hidden.user.domain.model.SysOrg;
import com.snowy.hidden.user.domain.model.SysUser;
import com.snowy.hidden.user.mapper.AppletUserMapper;
import com.snowy.hidden.user.mapper.SysOrgMapper;
import com.snowy.hidden.user.mapper.SysRoleMapper;
import com.snowy.hidden.user.mapper.SysUserMapper;
import com.snowy.hidden.user.service.AppletUserService;
import com.snowy.hidden.user.service.SysRoleService;
import com.snowy.hidden.util.CommonCryptogramUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序用户信息(AppletUser)表服务实现类
 *
 * @author h
 * @since 2025-10-21 17:17:15
 */
@Service("appletUserService")
public class AppletUserServiceImpl extends ServiceImpl<AppletUserMapper, AppletUser> implements AppletUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysOrgMapper sysOrgMapper;
    @Resource
    private SysRoleService sysRoleService;

    @Override
    public LoginUser getByOpenIdAndPhone(String openId, String phone) {
        AppletUser appletUser = this.baseMapper.getByOpenId(openId);
        if(ObjectUtil.isEmpty(appletUser)){
            appletUser = new AppletUser();
            appletUser.setOpenId(openId);
            appletUser.setInnerAccount(0);
            this.baseMapper.insert(appletUser);
        }
        LoginUser loginUser = new LoginUser();
        if(ObjectUtil.isNotEmpty(appletUser)){
            BeanUtils.copyProperties(appletUser,loginUser);
            loginUser.setUserName(appletUser.getNickName());
            // API无需权限码
            loginUser.setPermissionCodeList(CollectionUtil.newArrayList());
            // API无需数据权限范围
            loginUser.setDataScopeList(CollectionUtil.newArrayList());
            List<String> roleCodeList = this.sysRoleService.listUserRoleCode(appletUser.getPlatformUserId());
            loginUser.setRoleCodeList(roleCodeList);
            return loginUser;
        }
        // 查询是否内部员工
        SysUser sysUser = sysUserMapper.getByPhone(CommonCryptogramUtil.doSm4CbcEncrypt(phone));
        if(ObjectUtil.isNotEmpty(sysUser)){
            SysOrg sysOrg = sysOrgMapper.selectById(sysUser.getOrgId());
            appletUser.setInnerAccount(1);
            appletUser.setPlatformUserId(sysUser.getId());
            appletUser.setOrgId(sysUser.getOrgId());
            appletUser.setOrgName(sysOrg.getName());
            this.updateById(appletUser);
        }
        BeanUtils.copyProperties(appletUser,loginUser);
        // API无需权限码
        loginUser.setPermissionCodeList(CollectionUtil.newArrayList());
        // API无需数据权限范围
        loginUser.setDataScopeList(CollectionUtil.newArrayList());
        List<String> roleCodeList = this.sysRoleService.listUserRoleCode(appletUser.getPlatformUserId());
        loginUser.setRoleCodeList(roleCodeList);
        return loginUser;
    }
}
